package com.arox.springinaction.post;

import org.apache.activemq.pool.PooledConnectionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PostListenerMain {
    public static String FRONTDESK_IMPL = "frontDeskTemplate";

    public static void main(String[] args) {
	final ApplicationContext appContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
	final FrontDesk frontDesk = (FrontDesk) appContext.getBean(FRONTDESK_IMPL);
	Thread frontDeskTh = new Thread(new Runnable() {
	    public void run() {
		for (int i = 0; i < 10; i++) {
		    Mail mail = new Mail("Mail No. " + i, "US", 1.5);
		    frontDesk.sendMail(mail);
		    System.out.println("Mail #" + mail.getMailId() + " sent!");
		    try {
			Thread.sleep(1000);
		    } catch (InterruptedException ex) {
			frontDesk.sendMail(new Mail("Error mail", "US", 1.5));
		    }
		}
	    }
	}, "frontDeskTh");
	frontDeskTh.start();
	try {
	    frontDeskTh.join();
	} catch (InterruptedException ex) {
	}
	// Stop the Connections
	PooledConnectionFactory connectionFactory = (PooledConnectionFactory) appContext.getBean("connectionFactory");
	connectionFactory.stop();
    }
}
